package cn.cxl.cxlutils;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.net.http.SslError;
import android.os.Build;
import android.os.Environment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;

import java.io.File;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import cn.cxl.cxlutils.utils.LogcatFileManager;
import cn.cxl.cxlutils.utils.MyLog;
import cn.cxl.cxlutils.utils.NetworkUtils;
import cn.cxl.cxlutils.widget.CustomView;
import cn.cxl.cxlutils.widget.MyView;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.android.FlutterView;


public class MainActivity extends AppCompatActivity {
    @BindView(R.id.customView)
    CustomView customView;
    @BindView(R.id.invalid)
    InvalidTextView invalid;
    @SuppressLint("ObjectAnimatorBinding")
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        final WebView webviewNotice = findViewById(R.id.webview_notice);
                final WebView webviewNotice2 = findViewById(R.id.webview_notice2);

        MyLog.e("MainActivity","开始");
        webSetting(webviewNotice);
        webviewNotice.loadUrl( "https://www.xinanshizu.com/appSearchScreen/screenContentHtml?screenPkey=5343"  );
        webviewNotice2.loadUrl("https://ie.icoa.cn/");

        customView.smoothScrollTo(-400,0);

        PropertyValuesHolder valuesHolder1=PropertyValuesHolder.ofFloat("scaleX",1f,2f);
        PropertyValuesHolder valuesHolder2=PropertyValuesHolder.ofFloat("rotationX",0f,90f,0f);
        PropertyValuesHolder valuesHolder3=PropertyValuesHolder.ofFloat("alpha",1f,0.3f,1f);
        ObjectAnimator objectAnimator1=ObjectAnimator.ofPropertyValuesHolder(customView,valuesHolder1
                ,valuesHolder2,valuesHolder3);
//        objectAnimator1.setDuration(2000).start();


        @SuppressLint("ObjectAnimatorBinding")
        ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(customView,"translationX",200);
//        objectAnimator.setDuration(2000);
//        objectAnimator.start();
        ObjectAnimator objectAnimator2= ObjectAnimator.ofFloat(customView,"scaleX",1f,2f);
        ObjectAnimator objectAnimator3= ObjectAnimator.ofFloat(customView,"rotationX",0f,90f,0f);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(4000);
        animatorSet.play(objectAnimator).after(objectAnimator2).with(objectAnimator3).after(objectAnimator1);
        animatorSet.start();


        Animator animator = AnimatorInflater.loadAnimator(this, R.animator.scale);
        animator.setTarget(customView);
        animator.start();

        MyView myView=new MyView( customView);
//     ObjectAnimator.ofInt(myView,"w",10).setDuration(2000).start();


        ValueAnimator valueAnimator=ValueAnimator.ofFloat(0,100);
        valueAnimator.setTarget(customView);
        valueAnimator.setDuration(1000).start();
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float f= (float) animation.getAnimatedValue();
                MyLog.e("ValueAnimator",f+"");

            }
        });

        objectAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }
        });


        invalid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                View flutterView  =new FlutterView(MainActivity.this);
//                ConstraintLayout.LayoutParams layout = new ConstraintLayout.LayoutParams(   ViewGroup.LayoutParams.MATCH_PARENT,
//                        ViewGroup.LayoutParams.MATCH_PARENT);
//
//                layout.leftMargin = 100;
//                layout.topMargin = 200;
//                addContentView(flutterView, layout);
                startActivity(
                        FlutterActivity.createDefaultIntent(MainActivity.this)
                );
            }
        });
        startActivity(
                FlutterActivity.createDefaultIntent(MainActivity.this)
        );
Thread.currentThread().isInterrupted();

    }


    /**
     * webView相关设置
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void webSetting(WebView webview ) {
        // 获取WebView的设置对象
        WebSettings settings = webview.getSettings();
        // 设置WebView显示放大和缩小的按钮
        //settings.setBuiltInZoomControls(true);
        // 设置双击放大和缩小
        //settings.setUseWideViewPort(true);
        // 设置可以加载JavaScript脚本
        settings.setJavaScriptEnabled(true);
        // 允许https中嵌套http  Android 5.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

        //在JsBindCard类里实现javascript想调用的方法，并将其实例化传入webview,
        // "AndroidWebView"这个字串告诉javascript调用哪个实例的方法
//        webview.addJavascriptInterface(new JsAndroidBannerConnection(this), "AndroidWebView");

        // 判断是否有网络，有网络不使用缓存，没有网络使用缓存
        if (NetworkUtils.checkNetwork(this)) {
            //不使用缓存
            settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        } else {
            settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        }

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // 强制让超链接在当前的WebView中打开
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
//                super.onReceivedSslError(view, handler, error);
                handler.proceed();

            }

            @Override
            public void onPageFinished(WebView view, String url) {
            }
        });


        webview.setWebChromeClient(new WebChromeClient() {


            /**
             * 加载进度改变时调用
             * @param view 当前的WebView对象
             * @param newProgress 当前的进度
             */
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (100 <= newProgress) {

                } else {

                }
            }

            /**
             * 正在加载的标题
             * @param view WebView对象
             * @param title 加载的标题
             */
            @Override
            public void onReceivedTitle(WebView view, String title) {
                if (!TextUtils.isEmpty(title) && title.toLowerCase().contains("error")) {
                }
            }
        });
    }

}
